package com.evan.review.launchmode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * 单实例模式(singleInstance)的Activity演示
 * 
 * 特点：
 * 1. 该Activity独自占用一个任务栈，且任务栈中只有它一个Activity
 * 2. 整个系统中只存在一个该Activity的实例
 * 3. 适用于需要与应用程序分离的页面，如来电显示、闹钟提醒等
 */
public class SingleInstanceActivity extends AppCompatActivity {
    
    private static final String TAG = "SingleInstanceActivity";
    private TextView tvInfo;
    private int instanceCount = 0;
    private static int totalInstance = 0;
    private int onNewIntentCount = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singleinstance);
        
        // 记录实例数量
        instanceCount = ++totalInstance;
        
        initViews();
        updateInfo();
        
        Log.d(TAG, "onCreate: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    private void initViews() {
        tvInfo = findViewById(R.id.tv_info);
        
        Button btnCreateSelf = findViewById(R.id.btn_create_self);
        btnCreateSelf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 启动自身，测试单实例复用
                Intent intent = new Intent(SingleInstanceActivity.this, SingleInstanceActivity.class);
                startActivity(intent);
            }
        });
        
        Button btnCreateStandard = findViewById(R.id.btn_create_standard);
        btnCreateStandard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 启动一个标准Activity，观察任务栈变化
                Intent intent = new Intent(SingleInstanceActivity.this, StandardActivity.class);
                startActivity(intent);
            }
        });
        
        Button btnBackToMain = findViewById(R.id.btn_back_to_main);
        btnBackToMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 回到启动模式演示主界面
                Intent intent = new Intent(SingleInstanceActivity.this, LaunchModeActivity.class);
                startActivity(intent);
            }
        });
        
        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
    private void updateInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("【单实例模式演示】\n\n");
        sb.append("当前Activity: ").append(this.getClass().getSimpleName()).append("\n");
        sb.append("实例Hash: ").append(this.hashCode()).append("\n");
        sb.append("实例编号: ").append(instanceCount).append("/").append(totalInstance).append("\n");
        sb.append("onNewIntent调用次数: ").append(onNewIntentCount).append("\n");
        sb.append("任务栈ID: ").append(getTaskId()).append("\n\n");
        
        sb.append("特点说明:\n");
        sb.append("1. 单实例模式下，该Activity独占一个任务栈，且该栈中只有它一个Activity\n");
        sb.append("2. 整个系统中只有一个该Activity的实例\n");
        sb.append("3. 点击\"启动自身\"，复用当前实例并调用onNewIntent()\n");
        sb.append("4. 点击\"启动Standard\"，会在新的任务栈中创建Standard实例\n");
        sb.append("5. 点击\"回到主界面\"，会启动原来任务栈中的LaunchModeActivity\n\n");
        
        sb.append("返回栈实验:\n");
        sb.append("1. 从主界面进入SingleInstance，再点击\"启动Standard\"\n");
        sb.append("2. 点击返回键，不会回到SingleInstance，而是回到主界面\n");
        sb.append("3. 这是因为Standard与主界面位于同一任务栈，而SingleInstance在独立栈中\n\n");
        
        sb.append("日志查看提示:\n");
        sb.append("可在Logcat中过滤\"SingleInstanceActivity\"和\"taskId\"查看详细信息");
        
        tvInfo.setText(sb.toString());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        onNewIntentCount++;
        Log.d(TAG, "onNewIntent: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
        updateInfo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + this.toString() + ", taskId: " + getTaskId() + ", instance: " + instanceCount);
    }
} 